#include <boost/algorithm/string.hpp>
#include <boost/algorithm/string/predicate.hpp>
#include <boost/lexical_cast.hpp>
#include <iostream>
#include <fstream>
#include <algorithm>
#include <vector>

using namespace std;
using namespace boost;

int urlParser(){
	  ifstream myinputFile("/data1/team/weijiang/machine-learning-project-related/featuresGenerated/document_features.txt");
	  string line = "";
	  string x = "";
	  string host_name = "";
	  vector<string> elements;
	  if (myinputFile.is_open())
	  {
	    while ( myinputFile.good() )
	    {
	    	getline (myinputFile,line);
	    	if ( line != ""){
			      split(elements, line, is_any_of(" ") );

			      trim(elements[1]);
		    	  cout << elements[1] << endl;
		    	  x = elements[1];
		    	  size_t sp = x.find_first_of( '/', 7 /* skip http:// part */ );
		    	  if ( sp != string::npos ) {
		    	        string base_url( x.begin()+7, x.begin()+sp );
		    	        cout << base_url << endl;
		    	        if (boost::starts_with(base_url, "www.")){
		    	            host_name = base_url.substr(4,base_url.size());
		    	        	cout << host_name << endl;

		    	        }
		    	        else{
		    	        	host_name = base_url;
		    	        	cout << host_name << endl;
		    	        }
		    	        sp = x.find_last_of( '/' );
		    	        if ( sp != string::npos ) {
		    	                string query( x.begin()+sp+1, x.end() );
		    	                //cout << query << endl;
		    	        }
		    	  }
		    	  cout << endl;
	    	}

	    }
	    myinputFile.close();
	  }
	  else{
		  cout << "Unable to open file" << endl;
	  }

	  return 0;
}


string parseURLForDomainName(string inputURL){
	  string line = "";
	  string x = "";
	  string host_name = "";

			      trim(inputURL);
		    	  x = inputURL;
		    	  size_t sp = x.find_first_of( '/', 7 /* skip http:// part */ );
		    	  if ( sp != string::npos ) {
		    	        string base_url( x.begin()+7, x.begin()+sp );
		    	        cout << base_url << endl;
		    	        if (boost::starts_with(base_url, "www.")){
		    	            host_name = base_url.substr(4,base_url.size());
		    	        	//cout << host_name << endl;
		    	        }
		    	        else{
		    	        	host_name = base_url;
		    	        	//cout << host_name << endl;
		    	        }
		    	        sp = x.find_last_of( '/' );
		    	        if ( sp != string::npos ) {
		    	                string query( x.begin()+sp+1, x.end() );
		    	                //cout << query << endl;
		    	        }
		    	  }
		    	  //cout << endl;
	  return host_name;
}


int main()
{
  using namespace std;
  string x1 = "http://w1ww.google.co.in/search/search/?h=test&q=examaple";
  cout << x1 << endl;
  cout << parseURLForDomainName(x1) << endl;

  //urlParser();

  return 0;
}


